package cn.itcast.itcaststore.web.servlet.client;

import cn.itcast.itcaststore.domain.Orders;
import cn.itcast.itcaststore.domain.User;
import cn.itcast.itcaststore.serive.OrderService;
import cn.itcast.itcaststore.serive.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 通过用户信息查询订单
 *
 *
 */
@WebServlet("/findOrderByUser")
public class FindOrderByUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取名为“user”的session
        User user = (User) request.getSession().getAttribute("user");
        if (user==null){
            request.getRequestDispatcher("/error/privilege.jsp").forward(request, response);
        }
        // 调用service中的方法,根据用户信息查找订单
        OrderService service = new OrderServiceImpl();
        List<Orders> orders = null;
        try {
            orders = service.findOrderByUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("orders", orders);
        request.getRequestDispatcher("/client/orderlist.jsp").forward(request, response);
    }


}
